package com.compomics.proteocloud.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileProcessor {
	/**
	 * Holds the MGF files.
	 */
	private File[] files;
	
	/**
	 * This variable holds the initial chunksize;
	 */
	private int chunkSize;
	
	/**
	 * This variable holds the offset for the chunk sizing.
	 */
	private int offset;

    /**
     * This variable holds the chunk folder.
     */
    private File folder;
    
	/**
	 * Holds the number of chunks.
	 */
	private int chunkNumber;

    /**
     * The chunked files.
     */
    private List<File> chunkedFiles;


    public FileProcessor(File[] files, File folder, int chunksize) {
		this.files = files;
		this.chunkSize = chunksize;
        this.folder = folder; 
	}	
	
	/**
	 * Write the chunked/merged output files.
	 * @throws IOException
	 */
	public void write() throws IOException {
		File output;
        final String path;
		String outputFilename, line, filename;
		int start, spectrumCounter = 0;
		int initSize = chunkSize;
		chunkNumber = 1;
		BufferedReader br = null;
		BufferedWriter bos = null;
        path = folder.getAbsolutePath();

        chunkedFiles = new ArrayList<File>();
		// Iterate over all the files.
		for (int i = 0; i < files.length; i++) {
			br = new BufferedReader(new FileReader(files[i]));
		
			// Write new output file for the first file.
			if(i == 0) {
				// Read the filename.
				filename = files[i].getName();

				start = filename.lastIndexOf(".");
				outputFilename = filename.substring(0, start) + "_" + chunkNumber + filename.substring(start);
			    //	outputFilename = outputFilename.replaceAll(" ", "");
				output = new File(path + File.separator + outputFilename);
                chunkedFiles.add(output);
				bos = new BufferedWriter(new FileWriter(output));				
			} 
			
			// Cycle the file.
			while ((line = br.readLine()) != null) {
				line = line.trim();
				bos.write(line);
				bos.newLine();
				if (line.indexOf("END IONS") >= 0) {				
					// Increment the spectrumCounter by one.
					spectrumCounter++;
					
					// Each specified offset the file gets chunked.
					if (spectrumCounter % chunkSize == 0){				
		        		chunkNumber++;
		        		bos.flush();
		        		bos.close();
		        		filename = files[i].getName();		        		
		        		start = filename.lastIndexOf(".");
		                outputFilename = filename.substring(0, start) + "_" + chunkNumber + filename.substring(start);
		                //outputFilename = outputFilename.replaceAll(" ", "");						
		        		output = new File(path + File.separator + outputFilename);
                        chunkedFiles.add(output);
		        		bos = new BufferedWriter(new FileWriter(output));
		        	}
				}
			}	
		}				
		br.close();
		bos.flush();
        bos.close();
	}
	
	/**
	 * Returns the number of chunks.
	 * @return chunkNumber
	 */
	public int getChunkNumber() {
		return chunkNumber;
	}

    public List<File> getChunkedFiles() {
        return chunkedFiles;
    }
}
